package com.qfedu.mall.service.Impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.mall.beans.entity.Product;
import com.qfedu.mall.beans.entity.ProductDetail;
import com.qfedu.mall.beans.entity.ProductSku;
import com.qfedu.mall.beans.vo.ProductCommentsVo;
import com.qfedu.mall.beans.vo.ProductSkuVO;
import com.qfedu.mall.beans.vo.ProductVO;
import com.qfedu.mall.common.vo.*;
import com.qfedu.mall.mapper.ProductMapper;
import com.qfedu.mall.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
@Service
@Slf4j
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper mapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVO selectProductByDiscounts(Integer discounts) {
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("discounts",discounts);
        List<Product> products = mapper.selectByExample(example);
        return new ResultVO(ResStatus.OK,"success",products);
    }

    /**
     * author: dyw
     * @param category
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVO ProductCate(Integer category) {
        System.out.println("category:"+category);
        List<Product> list = mapper.seleProductByCate(category);
        log.info("结果集：{}",list);
        ResultVO resultVO = new ResultVO(ResStatus.OK,"Success",list);

        return resultVO;
    }


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVO Pin() {

        List<PinVo> pinList = mapper.selectAllShareBill();
        log.info("结果集：{}",pinList);
        return new ResultVO(ResStatus.OK,"Success",pinList);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVO detailInfo(Integer pid) {
       /* System.out.println("---------------");
        System.out.println(pid);*/
        final ResultVO resultVO = new ResultVO();

        try {

            List<Product> pro = mapper.seleProductBypid(pid);

            resultVO.setData(pro);
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");
        } catch (Exception e) {
            e.printStackTrace();
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("服务器端异常");
        }

        return resultVO;
    }
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVO selectcomments(Integer pid) {
        final ResultVO resultVO = new ResultVO();

        try {

            List<ProductCommentsVo> productCommentsVos = mapper.selecommentsBypid(pid);

            resultVO.setData(productCommentsVos);
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");
        } catch (Exception e) {
            e.printStackTrace();
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("服务器端异常");
        }
        return resultVO;
    }

    @Override
    public ResultVO shopDetails() {
        ProductDetail productDetail = mapper.selectShopDetail();

        List<String> imgList = new ArrayList<String>();
        imgList.add(productDetail.getImg1());
        imgList.add(productDetail.getImg2());
        imgList.add(productDetail.getImg3());

        ResultVO resultVO = new ResultVO(ResStatus.OK,"Success",imgList);
        return resultVO;
    }




    @Override
    public ResultVO selectProductSku(Integer pid) {
        final ResultVO resultVO = new ResultVO();

        try {

            List<ProductSku> psku = mapper.selectSkuBypid(pid);

            resultVO.setData(psku);
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");
        } catch (Exception e) {
            e.printStackTrace();
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("服务器端异常");
        }
        return resultVO;
    }
    @Override
    public ResultVO insertByPVO(ProductVO productVO) {
        final ResultVO resultVO = new ResultVO();

        int a = mapper.insertProduct(productVO);

        if(a>0){
            List<ProductVO> product1 = mapper.getProductAll();
            resultVO.setData(product1);
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");
            return resultVO;
        }else {
            List<ProductVO> product1 = mapper.getProductAll();
            resultVO.setData(product1);
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("插入失败");
            return resultVO;}
    }

    @Override
    public ResultVO deleteByPid(Integer pid) {
        final ResultVO resultVO = new ResultVO();

        int a = mapper.deleteProductB(pid);

        if(a>0){
            List<ProductVO> product1 = mapper.getProductAll();
            resultVO.setData(product1);
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");
            return resultVO;
        }else {
            List<ProductVO> product1 = mapper.getProductAll();
            resultVO.setData(product1);
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("插入失败");
            return resultVO;}
    }

    @Override
    public ResultVO updateByPVO(ProductVO productVO) {
        final ResultVO resultVO = new ResultVO();
        int a = mapper.updateProduct(productVO);

        if(a>0){
            List<ProductVO> product1 = mapper.getProductAll();
            resultVO.setData(product1);
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");
            return resultVO;
        }else {
            List<ProductVO> product1 = mapper.getProductAll();
            resultVO.setData(product1);
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("插入失败");
            return resultVO;}
    }

    @Override
    public ResultVO insertBySVO(ProductSkuVO productSkuVO) {
        final ResultVO resultVO = new ResultVO();

        int a = mapper.insertProductSku(productSkuVO);

        if(a>0){
            List<ProductSkuVO> product2 = mapper.getProductSKUAll();
            resultVO.setData(product2);
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");
            return resultVO;
        }else {
            List<ProductSkuVO> product2 = mapper.getProductSKUAll();
            resultVO.setData(product2);
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("插入失败");
            return resultVO;}
    }

    @Override
    public ResultVO deleteSkuByPid(Integer sid) {
        final ResultVO resultVO = new ResultVO();

        int a = mapper.deleteProductSku(sid);

        if(a>0){
            List<ProductSkuVO> product2 = mapper.getProductSKUAll();
            resultVO.setData(product2);
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");
            return resultVO;
        }else {
            List<ProductSkuVO> product2 = mapper.getProductSKUAll();
            resultVO.setData(product2);
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("删除失败");
            return resultVO;}
    }

    @Override
    public ResultVO updateBySVO(ProductSkuVO productSkuVO) {
        final ResultVO resultVO = new ResultVO();

        int a = mapper.updateProductSku(productSkuVO);

        if(a>0){
            List<ProductSkuVO> product2 = mapper.getProductSKUAll();
            resultVO.setData(product2);
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");
            return resultVO;
        }else {
            List<ProductSkuVO> product2 = mapper.getProductSKUAll();
            resultVO.setData(product2);
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("更改失败");
            return resultVO;}
    }

    @Override
    public ResultVO1 getAllShops(PageVo pageVo) {

        PageHelper.startPage(pageVo.getPage(),pageVo.getLimit());
        List<ProductVO> products = mapper.getProductAll();
        products.forEach(System.out::println);
        PageInfo pageInfo = new PageInfo(products);
        System.out.println("商品pageInfo" + pageInfo);

        List<ProductVO> products1 = mapper.getProductAll();


        return new ResultVO1(ResStatus.OK,products1,pageInfo);
    }
}
